﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类InboxDAO。
    /// </summary>
    public class InboxDAO
    {
        public InboxDAO()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int MsgID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@MsgID", SqlDbType.Int,4)};
            parameters[0].Value = MsgID;

            int result = DbHelperSQL.RunProcedure("UP_Inbox_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public int Add(TimKen.DataEntity.Inbox model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@MsgID", SqlDbType.Int,4),
					new SqlParameter("@MsgNo", SqlDbType.VarChar,50),
					new SqlParameter("@Title", SqlDbType.VarChar,200),
					new SqlParameter("@FromID", SqlDbType.VarChar,32),
					new SqlParameter("@ToID", SqlDbType.VarChar,32),
					new SqlParameter("@Message", SqlDbType.NVarChar,2000),
					new SqlParameter("@FromDelete", SqlDbType.Bit,1),
					new SqlParameter("@ToDelete", SqlDbType.Bit,1),
					new SqlParameter("@IsRead", SqlDbType.Bit,1),
					new SqlParameter("@ToAll", SqlDbType.Int,4),
					new SqlParameter("@CreateDate", SqlDbType.DateTime)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.MsgNo;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.FromID;
            parameters[4].Value = model.ToID;
            parameters[5].Value = model.Message;
            parameters[6].Value = model.FromDelete;
            parameters[7].Value = model.ToDelete;
            parameters[8].Value = model.IsRead;
            parameters[9].Value = model.ToAll;
            parameters[10].Value = model.CreateDate;

            DbHelperSQL.RunProcedure("UP_Inbox_ADD", parameters, out rowsAffected);
            return (int)parameters[0].Value;
        }

        public void AddAll(TimKen.DataEntity.Inbox model)
        {
            SqlParameter[] parameters = {
	                    new SqlParameter("@MsgNo", SqlDbType.VarChar,50),
					new SqlParameter("@Title", SqlDbType.VarChar,200),
					new SqlParameter("@FromID", SqlDbType.VarChar,32),
					new SqlParameter("@Message", SqlDbType.NVarChar,2000),
					new SqlParameter("@FromDelete", SqlDbType.Bit,1),
					new SqlParameter("@ToDelete", SqlDbType.Bit,1),
					new SqlParameter("@IsRead", SqlDbType.Bit,1),
					new SqlParameter("@ToAll", SqlDbType.Int,4),
					new SqlParameter("@CreateDate", SqlDbType.DateTime)};
            parameters[0].Value = model.MsgNo;
            parameters[1].Value = model.Title;
            parameters[2].Value = model.FromID;
            parameters[3].Value = model.Message;
            parameters[4].Value = model.FromDelete;
            parameters[5].Value = model.ToDelete;
            parameters[6].Value = model.IsRead;
            parameters[7].Value = model.ToAll;
            parameters[8].Value = model.CreateDate;

            DbHelperSQL.RunProcedure("UP_Inbox_ADDALL", parameters);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.Inbox model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@MsgID", SqlDbType.Int,4),
					new SqlParameter("@MsgNo", SqlDbType.VarChar,50),
					new SqlParameter("@Title", SqlDbType.VarChar,200),
					new SqlParameter("@FromID", SqlDbType.VarChar,32),
					new SqlParameter("@ToID", SqlDbType.VarChar,32),
					new SqlParameter("@Message", SqlDbType.NVarChar,2000),
					new SqlParameter("@FromDelete", SqlDbType.Bit,1),
					new SqlParameter("@ToDelete", SqlDbType.Bit,1),
					new SqlParameter("@IsRead", SqlDbType.Bit,1),
					new SqlParameter("@ToAll", SqlDbType.Int,4),
					new SqlParameter("@CreateDate", SqlDbType.DateTime)};
            parameters[0].Value = model.MsgID;
            parameters[1].Value = model.MsgNo;
            parameters[2].Value = model.Title;
            parameters[3].Value = model.FromID;
            parameters[4].Value = model.ToID;
            parameters[5].Value = model.Message;
            parameters[6].Value = model.FromDelete;
            parameters[7].Value = model.ToDelete;
            parameters[8].Value = model.IsRead;
            parameters[9].Value = model.ToAll;
            parameters[10].Value = model.CreateDate;

            DbHelperSQL.RunProcedure("UP_Inbox_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int MsgID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@MsgID", SqlDbType.Int,4)};
            parameters[0].Value = MsgID;

            DbHelperSQL.RunProcedure("UP_Inbox_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="MsgNo"></param>
        /// <param name="DeleteType">1为从发件箱删除，0为从收件箱删除</param>
        public void Delete(string MsgNo, int DeleteType)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@MsgNo", SqlDbType.VarChar),
					new SqlParameter("@DeleteType", SqlDbType.Int,4)};
            parameters[0].Value = MsgNo;
            parameters[1].Value = DeleteType;

            DbHelperSQL.RunProcedure("UP_Inbox_DeleteFromShow", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.Inbox GetModel(int MsgID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@MsgID", SqlDbType.Int,4)};
            parameters[0].Value = MsgID;

            TimKen.DataEntity.Inbox model = new TimKen.DataEntity.Inbox();
            DataSet ds = DbHelperSQL.RunProcedure("UP_Inbox_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["MsgID"].ToString() != "")
                {
                    model.MsgID = int.Parse(ds.Tables[0].Rows[0]["MsgID"].ToString());
                }
                model.MsgNo = ds.Tables[0].Rows[0]["MsgNo"].ToString();
                model.Title = ds.Tables[0].Rows[0]["Title"].ToString();
                model.FromID = ds.Tables[0].Rows[0]["FromID"].ToString();
                model.ToID = ds.Tables[0].Rows[0]["ToID"].ToString();
                model.Message = ds.Tables[0].Rows[0]["Message"].ToString();
                if (ds.Tables[0].Rows[0]["FromDelete"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["FromDelete"].ToString() == "1") || (ds.Tables[0].Rows[0]["FromDelete"].ToString().ToLower() == "true"))
                    {
                        model.FromDelete = true;
                    }
                    else
                    {
                        model.FromDelete = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["ToDelete"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["ToDelete"].ToString() == "1") || (ds.Tables[0].Rows[0]["ToDelete"].ToString().ToLower() == "true"))
                    {
                        model.ToDelete = true;
                    }
                    else
                    {
                        model.ToDelete = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["IsRead"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsRead"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsRead"].ToString().ToLower() == "true"))
                    {
                        model.IsRead = true;
                    }
                    else
                    {
                        model.IsRead = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["ToAll"].ToString() != "")
                {
                    model.ToAll = int.Parse(ds.Tables[0].Rows[0]["ToAll"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(ds.Tables[0].Rows[0]["CreateDate"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select MsgID,MsgNo,Title,FromID,ToID,Message,FromDelete,ToDelete,IsRead,ToAll,CreateDate ");
            strSql.Append(" FROM Inbox ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据总数
        /// </summary>
        public DataSet GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(MsgID) as total ");
            strSql.Append(" FROM Inbox ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string ToID, string FromID, int IsRead)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@OrderfldName", SqlDbType.VarChar,255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@ToID ", SqlDbType.VarChar,50),
                    new SqlParameter("@FromID ", SqlDbType.VarChar,50),
                    new SqlParameter("@IsRead  ", SqlDbType.Int,50)
                    };
            parameters[0].Value = "MsgID";
            parameters[1].Value = PageSize;
            parameters[2].Value = PageIndex;
            parameters[3].Value = 1;
            parameters[4].Value = 1;
            parameters[5].Value = ToID;
            parameters[6].Value = FromID;
            parameters[7].Value = IsRead;
            return DbHelperSQL.RunProcedure("UP_Inbox_GetPageList", parameters, "ds");
        }

        #endregion  成员方法
    }
}

